<%--
  Created by IntelliJ IDEA.
  User: 30822
  Date: 2023/6/13
  Time: 21:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--添加器材--%>
<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">器材名称</label>
        <div class="layui-input-block">
            <input style="width: 150px" type="text" name="equipmentName" required lay-verify="required" placeholder="请输入器材名称" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">器材位置</label>
        <div class="layui-input-block">
            <input  style="width: 150px" type="text" name="equipmentCation" required lay-verify="required" placeholder="请输入器材位置" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">器材数量</label>
        <div class="layui-input-block">
            <input style="width: 150px" type="number" name="equipmentNum" required lay-verify="required|number" placeholder="请输入器材数量" autocomplete="off" class="layui-input">
        </div>
    </div>

<%--    <div class="layui-form-item">--%>
<%--   <input type="hidden" name="avatar" id="my-input">--%>
<%--    </div>--%>

    <div style="width: 150px;margin-left: 50px;margin-bottom: 50px">
        <div class="layui-form-item" style="margin-left: -49px">
            <label class="layui-form-label">器材图片</label>
            <input type="hidden" name="avatar" id="MyImg">
        </div>
        <button type="button" class="layui-btn" id="ID-upload-demo-btn">
            <i class="layui-icon layui-icon-upload"></i> 图片上传
        </button>
        <div class="layui-upload-list">
            <img class="layui-upload-img" id="ID-upload-demo-img" style="width: 100%; height: 92px;">
        </div>
        <div class="layui-progress layui-progress-big" lay-showPercent="yes" lay-filter="filter-demo">
            <div class="layui-progress-bar" lay-percent=""></div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script>

    layui.use(['layer','upload'], function() {
        var upload = layui.upload;
        var layer = layui.layer;
        var element = layui.element;
        var $ = layui.$;
        // 单图片上传
        var uploadInst = upload.render({
            elem: '#ID-upload-demo-btn',
            url: '${pageContext.request.contextPath}/equipment//uploadFile', // 此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            before: function(obj){
                // 预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#ID-upload-demo-img').attr('src', result); // 图片链接（base64）
                });

                element.progress('filter-demo', '0%'); // 进度条复位
                layer.msg('上传中', {icon: 16, time: 0});
            },
            done: function(res){
                // 若上传失败
                if(res.code == 200){

                    const input = document.getElementById("MyImg");
                    // 设置 input 元素的内容
                    input.value = res.msg;
                    console.log(res.msg);
                    return layer.msg('上传成功');

                    // 通过 ID 选择器获取 input 元素


                }else{
                    return layer.msg('上传失败');
                }
                // 上传成功的一些操作
                // …
                $('#ID-upload-demo-text').html(''); // 置空上传失败的状态
            },
            error: function(){
                // 演示失败状态，并实现重传
                var demoText = $('#ID-upload-demo-text');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            },
            // 进度条
            progress: function(n, elem, e){
                element.progress('filter-demo', n + '%'); // 可配合 layui 进度条元素使用
                if(n == 100){
                    layer.msg('上传完毕', {icon: 1});
                }
            }
        });
    });


    layui.use(['layer','upload','form'], function() {
        var layer = layui.layer;
        //定义表单，为了获取表单数据
        let form = layui.form;
        var upload = layui.upload;
        var element = layui.element;
        var $ = layui.$;
        form.on('submit(formDemo)', function(data){
            // TODO：提交表单后的处理逻辑
            // data.field.id = id

            $.ajax({
                url: '${pageContext.request.contextPath}/equipment/addEquipment',
                type: "post",
                data: data.field,
                success: function (result) {

                    toMemberInfo('${pageContext.request.contextPath}/equipment/ToEquipment');

                    layer.msg('添加成功',{icon: 1});

                },
                error: function () {
                    layer.msg('添加失败',{icon: 2});
                }

            });
            // layer.close(index); // 关闭弹出层

            return false; // 阻止表单跳转
        });
    });
</script>
</body>
</html>
